package com.appkubes.fintech.restful.application.sms.controller;

import com.aliyuncs.exceptions.ClientException;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.restful.application.sms.entity.vo.SmsPhoneVO;
import com.appkubes.fintech.restful.application.sms.service.SmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;

/**
 * @Author: wuzhijian
 * @Date: 2019/12/26
 * @Description: 获取短信验证码
 */
@Api(tags = "短信验证码")
@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private SmsService smsService;

    @ApiOperation("获取手机短信验证码")
    @GetMapping("/send")
    public Response getMessageCode(@Valid SmsPhoneVO smsPhoneVO, BindingResult bindingResult) throws IOException, ClientException {
        if (bindingResult.hasErrors()) {
            return Response.fail("参数检查失败");
        }
        String code = smsService.send(smsPhoneVO.getPhone());
        if (code == null) {
            return Response.fail("获取验证码失败，请重新获取！");
        }
        return Response.success();
    }

    @ApiOperation("验证手机验证码")
    @GetMapping("/valid")
    public Response phoneCredit(String phone, String code) {
        boolean result = smsService.valid(phone, code);
        //手机验证码错误
        return Response.success(result);
    }

}
